﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text.RegularExpressions;
using RenChangYou.Common;
using bll = RenChangYou.BLL;
using mod = RenChangYou.Model;
using RenChangYou.BLL;
using System.Data;
using DaoYou.Web.Guider.Account;


namespace DaoYou.Web.Account
{
    public partial class Register : System.Web.UI.Page
    {

        protected void Page_Load(object sender, EventArgs e)
        {
        }

        private bool ValidateInputData()
        {
            bool regFlag = true;

            if (!this.ckbAgreement.Checked)
            {
                Common.AlertMessage("请阅读并同意注册协议！");
                return false;
            }

            //验证账号
            if (Regex.IsMatch(txt_userName.Value.Trim(), "^[a-zA-Z0-9]{4,15}$"))
                regFlag = regFlag & true;
            else
            {
                Common.AlertMessage("用户名英文或数字,长度为4~14的字符！");
                return false;
            }

            //验证密码
            if (Regex.IsMatch(txt_Password.Value.Trim(), "^[a-zA-Z0-9]{6,16}$"))
                regFlag = regFlag & true;
            else
            {
                Common.AlertMessage("密码不符合规范，密码必须为6~16位英文+数字的组合！");
                return false;
            }

            if (txt_Password.Value.Trim() == txt_RePassword.Value.Trim())
                regFlag = regFlag & true;
            else
            {
                Common.AlertMessage("确认密码与密码不一致，请重新填写");
                return false;
            }



            return regFlag;
        }

        protected void btn_Register_Click(object sender, EventArgs e)
        {
            try
            {
                if (ValidateInputData())
                {
                    mod.DY_User mod_User = new mod.DY_User();
                    mod_User.UserName = txt_userName.Value.Trim();
                    mod_User.Password = txt_Password.Value.Trim();
                    mod_User.UserType = this.cbkIsGuider.Checked ? 1 : 0;

                    bll.DY_User bll_user = new bll.DY_User();
                    int ret = bll_user.RegisterUser(mod_User);
                    if (ret == 1)
                    {
                        DY_User bll_User = new DY_User();
                        using (DataTable dt = bll_User.UserLogin(txt_userName.Value.Trim(), txt_Password.Value.Trim()))
                        {
                            if (!Common.DtIsNullOrEmpty(dt))
                            {
                                PageHelper.Current.SetCookie(dt.Rows[0]["UserName"].ToString(), Convert.ToInt32(dt.Rows[0]["UserType"]));
                            }
                        }

                        string url = Page.ResolveUrl("~/Account/UserCenter.aspx");

                        if (HttpContext.Current.Request.QueryString["ReturnUrl"] != null)
                        {
                            url = HttpContext.Current.Request.QueryString["ReturnUrl"];
                        }

                        Common.AlertMessageAndRedirect("注册成功，请继续完善您的个人信息！", url);
                    }
                    else if (ret == 0)
                    {
                        Common.AlertMessage("用户名已经存在，请重新注册！");
                    }
                    else
                    {
                        Common.AlertMessage("系统错误请稍候再试！");
                    }
                }
            }
            catch (Exception ex)
            {
                Common.AlertMessage("系统错误请稍候再试！" + ex.Message);
                return;
            }
        }
    }


}
